package com.example.word_book.service;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.word_book.R;
import com.example.word_book.utils.SqlHelp;

public class RegisterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);


        init();
    }

    private void init() {
        ImageView back = findViewById(R.id.img_back);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        EditText account, password,db_password;
        account = findViewById(R.id.edt_account);

        password = findViewById(R.id.edt_password);

        db_password = findViewById(R.id.edt_again_password);

        Button btn = findViewById(R.id.btn_register);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String s_account = account.getText().toString();
                String s_password = password.getText().toString();
                String sdb_password = db_password.getText().toString();

                if (s_account.equals("") || s_password.equals("") || sdb_password.equals("")){
                    Toast.makeText(RegisterActivity.this, "请输入账号或密码", Toast.LENGTH_SHORT).show();
                }else if(!sdb_password.equals(s_password)) {
                    Toast.makeText(RegisterActivity.this, "两次密码不相同", Toast.LENGTH_SHORT).show();
                    //判断是否输入框输入的内容
                }else {
                    if (new SqlHelp(RegisterActivity.this).insertUser(s_account,s_password)) {

                        Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                        finish();
                    }else {
                        Toast.makeText(RegisterActivity.this, "已存在相同用户名", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }
}